/*
 * RegimenBusquedaFrame.java
 *
 * Created on 22 de abril de 2008, 02:15 PM
 */
package org.contabilidad.regimen;


import org.mybeans.Sesion;
import org.mybeans.RegimenBeans;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author  avbravo
 */
public class RegimenBusquedaFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    RegimenBeans regimenBeans;

    /** Creates new form RegimenBusquedaFrame */
    public RegimenBusquedaFrame() {
        initComponents();
           /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */

        sesion = new Sesion();
        regimenBeans = new RegimenBeans();
        TableColumn columnId = jTable1.getColumnModel().getColumn(0);
        columnId.setPreferredWidth(70);
        TableColumn column = jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(250);
    String sql = "select * from regimen where idmunicipio='" + sesion.getIdmunicipio() + "' and regimen.activo ='si' order by idregimen";
    CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldRegimen = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldIdRegimen = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonBuscarCodigo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonVerTodos = new javax.swing.JButton();
        jTextFieldIdSubGrupoRegimen = new javax.swing.JTextField();
        jButtonBuscarSubGrupo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Busqueda de Regimen");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Regimen");

        jTextFieldRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRegimenActionPerformed(evt);
            }
        });

        jLabel3.setText("Codigo");

        jTextFieldIdRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdRegimenActionPerformed(evt);
            }
        });

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/consultar.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Consultar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonBuscarCodigo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/consultar.png"))); // NOI18N
        jButtonBuscarCodigo.setToolTipText("Consultar");
        jButtonBuscarCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarCodigoActionPerformed(evt);
            }
        });

        jLabel1.setText("Sub Grupo");

        jButtonVerTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/view.png"))); // NOI18N
        jButtonVerTodos.setToolTipText("Ver Todos");
        jButtonVerTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerTodosActionPerformed(evt);
            }
        });

        jTextFieldIdSubGrupoRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdSubGrupoRegimenActionPerformed(evt);
            }
        });

        jButtonBuscarSubGrupo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/consultar.png"))); // NOI18N
        jButtonBuscarSubGrupo.setToolTipText("Consultar");
        jButtonBuscarSubGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarSubGrupoActionPerformed(evt);
            }
        });

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Regimen", "Valor", "SubGrupo"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addGap(22, 22, 22)
                                        .addComponent(jButtonVerTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addGap(1, 1, 1)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 466, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(jTextFieldIdRegimen)
                                                    .addComponent(jTextFieldIdSubGrupoRegimen, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jButtonBuscarCodigo)
                                                    .addComponent(jButtonBuscarSubGrupo)))))))
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonBuscar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 614, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonBuscarCodigo)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdSubGrupoRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscarSubGrupo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonVerTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed
 
    BuscarRegimen();

}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed
 
    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonVerTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerTodosActionPerformed
 
    String sql = "select * from regimen where idmunicipio='" + sesion.getIdmunicipio() + "' and regimen.activo ='si' order by idregimen";
    CargarTodosRegistros(sql);


}//GEN-LAST:event_jButtonVerTodosActionPerformed

private void jButtonBuscarCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarCodigoActionPerformed
 
BuscarCodigo();
}//GEN-LAST:event_jButtonBuscarCodigoActionPerformed

private void jButtonBuscarSubGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarSubGrupoActionPerformed
 
BuscarSubGrupo();
}//GEN-LAST:event_jButtonBuscarSubGrupoActionPerformed

private void jTextFieldRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRegimenActionPerformed
 
    BuscarRegimen();
}//GEN-LAST:event_jTextFieldRegimenActionPerformed

private void jTextFieldIdRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdRegimenActionPerformed
 
    BuscarCodigo();
}//GEN-LAST:event_jTextFieldIdRegimenActionPerformed

private void jTextFieldIdSubGrupoRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdSubGrupoRegimenActionPerformed
 
    BuscarSubGrupo();
}//GEN-LAST:event_jTextFieldIdSubGrupoRegimenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegimenBusquedaFrame().setVisible(true);
            }
        });
    }

    private void BuscarCodigo(){
            try {


        String idregimen = (String) this.jTextFieldIdRegimen.getText();
        String sql = "select * from regimen  where regimen.idregimen like '%" + idregimen + "%' and regimen.idmunicipio = '" + sesion.getIdmunicipio() + "' and regimen.activo ='si' order by idregimen";
        CargarTodosRegistros(sql);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    
    private void BuscarSubGrupo(){
            try {


        String idsubgruporegimen = (String) this.jTextFieldIdSubGrupoRegimen.getText();
        String sql = "select * from regimen  where regimen.idsubgruporegimen like '%" + idsubgruporegimen + "%' and regimen.idmunicipio = '" + sesion.getIdmunicipio() + "' and regimen.activo ='si' order by idregimen";
        CargarTodosRegistros(sql);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    private void BuscarRegimen(){
            try {


        String regimen = (String) this.jTextFieldRegimen.getText();
        String sql = "select * from regimen  where regimen.regimen like '%" + regimen + "%' and regimen.idmunicipio = '" + sesion.getIdmunicipio() + "'and regimen.activo ='si' order by regimen";
        CargarTodosRegistros(sql);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    private void CargarTodosRegistros(String sql) {
        try {

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();

            modelo = new DefaultTableModel();
            // Creamos las columnas.
            modelo.addColumn("codigo");
            modelo.addColumn("regimen");
            modelo.addColumn("valor");
            modelo.addColumn("subgrupo");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[4]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idregimen");
                fila[1] = rs.getString("regimen");
                fila[2] = rs.getString("valor");
                fila[3] = rs.getString("idsubgruporegimen");
                modelo.addRow(fila);

            }
            this.jTable1.setModel(modelo);
            jTable1.setVisible(true);
            //aris++
            this.jTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jTable1.rowAtPoint(e.getPoint());
                    int columna = jTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        jTextFieldIdRegimen.setText(String.valueOf(jTable1.getValueAt(fila, 0)));
                        jTextFieldRegimen.setText(String.valueOf(jTable1.getValueAt(fila, 1)));
                        jTextFieldIdSubGrupoRegimen.setText(String.valueOf(jTable1.getValueAt(fila, 3)));
                        String idregimen = (String) jTextFieldIdRegimen.getText();
                        String regimen = (String) jTextFieldRegimen.getText();

                        //  regimenBeans.setIdRegimen(idregimen);
                        //  regimenBeans.setRegimen(regimen);
                        BuscarDetallesRegimen(idregimen);

                    }
                }
            });


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void BuscarDetallesRegimen(String IdRegimen) {
        try {

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select regimen.* from regimen where regimen.idmunicipio = ? and idregimen = ?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, IdRegimen);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No existen registros de regimen con esa codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String activo = rs.getString("activo");
            if (activo.equals("no")) {
                JOptionPane.showMessageDialog(this, "Este regimen esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String IdSubGrupoRegimen = rs.getString("idsubgruporegimen");
            String Regimen = rs.getString("regimen");
            double Valor = rs.getDouble("valor");
            double LimiteInferior = rs.getDouble("limiteinferior");
            double LimiteSuperior = rs.getDouble("limiteSuperior");

            if (Regimen == null || Regimen.equals("")) {
                JOptionPane.showMessageDialog(this, "El regimen esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            String descripcion = rs.getString("descripcion");
            String observacion = rs.getString("observacion");
            regimenBeans.setIdRegimen(IdRegimen);
            regimenBeans.setIdSubGrupoRegimen(IdSubGrupoRegimen);
            regimenBeans.setRegimen(Regimen);
            regimenBeans.setValor(Valor);
            regimenBeans.setLimiteInferior(LimiteInferior);
            regimenBeans.setLimiteSuperior(LimiteSuperior);
            regimenBeans.setDescripcion(descripcion);
            regimenBeans.setObservacion(observacion);


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonBuscarCodigo;
    private javax.swing.JButton jButtonBuscarSubGrupo;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVerTodos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldIdRegimen;
    private javax.swing.JTextField jTextFieldIdSubGrupoRegimen;
    private javax.swing.JTextField jTextFieldRegimen;
    // End of variables declaration//GEN-END:variables
}
